package mall.service;

import mall.dao.MallIndexDao;
import mall.dao.MallIndexDaoImpl;
import mall.model.bo.MallAskGoodsMsgBO;
import mall.model.vo.*;

import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 10:49
 *
 * */
public class MallIndexServiceImpl implements MallIndexService {
    private MallIndexDao mallIndexDao = new MallIndexDaoImpl();
    @Override
    public List<MallShowGoodsVO> getGoodsByType(String typeId) {
        //获取所有商品信息
        if (typeId.equals("-1")) {
            return (List<MallShowGoodsVO>) mallIndexDao.getAllGoods();
        }
        //获取指定类别商品信息
        return mallIndexDao.getGoodsByType(typeId);
    }

    @Override
    public List<MallIndexSearchVO> searchGoods(String keyword) {
        return mallIndexDao.searchGoods(keyword);
    }

    @Override
    public MallGoodsInfoVO getGoodsInfo(String id) {
        MallGoodsInfoVO goodsInfoVO = mallIndexDao.getGoodsInfo(id);
        List<SpecsInfoVO> specsInfoVOS = mallIndexDao.getspecsInfoVOs(id);
        goodsInfoVO.setSpecs(specsInfoVOS);
        return goodsInfoVO;
    }

    @Override
    public List<MallGetGoodsMsgVO> getGoodsMsg(String id) {
        return mallIndexDao.getGoodsMsg(id);
    }

    @Override
    public MallShowCommentsVO getgoodsComment(String goodsId) {
        //查询当前id的所有评论
        List<MallShowCommentVO> commentVOS = mallIndexDao.getGoodsComment(goodsId);
        if (commentVOS.isEmpty()) {
            return new MallShowCommentsVO(commentVOS);
        }
        //计算好评率
        String rate = mallIndexDao.calculateRate(goodsId);
        return new MallShowCommentsVO(commentVOS, Double.parseDouble(rate));
    }

    @Override
    public void askGoodsMsg(MallAskGoodsMsgBO goodsMsgBO) {
        String userId = mallIndexDao.getUserIdFromUser(goodsMsgBO.getToken());
        String goodsName = mallIndexDao.getGoodsNameFromGoods(goodsMsgBO.getGoodsId());
        mallIndexDao.askGoodsMsg(goodsMsgBO, userId, goodsName);
    }

}
